﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Threading;
using System.Reflection; // 引用这个才能使用Missing字段
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using System.Runtime.InteropServices;

using ZedGraph;
using System.IO;
using System.Diagnostics;
using CCWin;


namespace weiqijiance
{
    public partial class TESTBR : Form
    {
        public string sshebei = "制动";
        public string sdetid;
        public bool cbjd;
        public mainform m_mainform;
        public 控制流程.flowcrtl flowbass = new 控制流程.flowcrtl();
        /// <summary>
        /// 结果变量
        /// </summary>
        public bool jcxjs = false;//检测线结束标识
        public double flunzhongzuo;//加载制动台轮重左
        public double flunzhongyou;//加载制动台轮重右

        public double fzhidonglizuo;//加载制动台制动左
        public double fzhidongliyou;//加载制动台制动右

        public double fJGlunzhongzuoJT;//静态轮重左
        public double fJGlunzhongyouJT;//静态轮重右

        public double fJGlunzhongzuo;//加载制动台测量值轮重左
        public double fJGlunzhongyou;//加载制动台测量值轮重右

        public double fJGzhidonglizuo;//加载制动台测量值制动左
        public double fJGzhidongliyou;//加载制动台测量值制动右

        public double fJGzhidongliGCCzuo;//加载过程差点左
        public double fJGzhidongliGCCyou;//加载过程差点右

        public float m_fspeedz;//第三滚筒信号左
        public float m_fspeedy;//第三滚筒信号右

        public bool bfszdcj = false;//自动复检
        public bool[] ixm = new bool[] { false, false, false, false, false, false };//加载制动完成标识
      

        public bool bBrakeFJZ;//加载制动率合格标志
        public bool bBrakeDBFJZ;//加载制动不平衡率合格标志

        public bool bBrakeF;//制动率合格标志
        public bool bBrakeDBF;//制动不平衡率合格标志


        public int izhidongHWQ=-1;//制动到位红外
        public int izhidongHWH=-1;//制动到位红外
        public int izhidongHWXX;//制动台下限红外
        public int izhidongHWSX;//制动台上限红外

        public int izhidongKZJSS;//制动举升升
        public int izhidongKZJSJ;//制动举升降

        public int izhidongJZQ = -1;//摩托车制动前夹子
        public int izhidongJZH = -1;//摩托车制动后夹子

        public int izhidongKZTJS;//制动台架升
        public int izhidongKZTJJ;//制动台架降


        public int izhidongKZDJZ;//制动电机左
        public int izhidongKZDJY;//制动电机右

        public int ilunzhongtdz;//轮重信号左通道
        public int ilunzhongtdy;//轮重信号右通道

        public int izhidongtdz;//制动信号左通道
        public int izhidongtdy;//制动信号右通道
        public int izhidongDSGTZ;//第三滚筒通道左
        public int izhidongDSGTY;//第三滚筒通道右


        public bool cdjc = false;//轮重制动重叠检测
        public bool jtlh = true;//静态轮荷
        public int ijushengshijian;//举升时间
        public int idianjishijiancha;//电机启动时间差
        public bool bzzlv = false;//是否需要阻滞率
        public bool bqyqhsxt = false;//启用前后摄像头
       
        public int fjcsj;//检测超时时间
        public int izs;//轴数
        public bool bzuihouyizhou;
        public string revert;
       
        public Thread t1;
        public string xianshineirong;
        public string businessId;
        public string jylsh;
        public string jyjgbh;
        public string jcxdh;
        public string jycs;
        public string jyxm;
        public bool bhz;

        public bool sdjcs;

        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;
        string ip;
        int port;

        public int panel_Width;
        public int panel_Height;
        SocketHelper socketClient;

        public TESTBR(int Width,int Height)
        {
            InitializeComponent();
            panel_Width = Width;
            panel_Height = Height;
            x = this.Width;
            y = this.Height;
            setTag(this);
            xinxixianshi.Text = "制动检测";
        }

        public void GetIP()
        {
            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);

            int.TryParse(objini.ReadValue("制动", "Port").Trim(), out port);
            ip = objini.ReadValue("制动", "IP").Trim();
        }

        public void shuaxinform(int Width, int Height)
        {
            panel_Width = Width;
            panel_Height = Height;
        }


        #region 控件大小随窗体大小等比例缩放
        private float x;//定义当前窗体的宽度
        private float y;//定义当前窗体的高度
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            if (newy <= 0)
            {
                newy = 1;
            }
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null )
                {
                    if (con.Dock == DockStyle.Top)
                        continue;
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        #endregion

        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }

        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }

        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(25500);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 25500, sPath);
            return temp.ToString();
        }

        private delegate void FlushClient();//代理
        private void xianshi()
        {
            if (this.xinxixianshi.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshi);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.xinxixianshi.Text = string.Format(@"{0}", xianshineirong);
            }
        }

        private void xianshizuo()
        {
            if (this.weight_left.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshizuo);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.weight_left.Visible = true;
                this.digitalGauge1.DigitCount = flunzhongzuo.ToString("0").Length < 3 ? 3 : flunzhongzuo.ToString("0").Length;
                this.digitalGauge1.Text = flunzhongzuo.ToString("0");
               
            }
        }

        private void xianshiyou()
        {
            if (this.weight_right.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshiyou);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.weight_right.Visible = true;
                this.digitalGauge3.DigitCount = flunzhongyou.ToString("0").Length < 3 ? 3 : flunzhongyou.ToString("0").Length;
                this.digitalGauge3.Text = flunzhongyou.ToString("0");
               
            }
        }

        private void xianshizhidongzou()
        {
            if (this.brake_left.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshizhidongzou);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.brake_left.Visible = true;
                this.digitalGauge2.DigitCount = fzhidonglizuo.ToString("0").Length < 3 ? 3 : fzhidonglizuo.ToString("0").Length;
                this.digitalGauge2.Text = fzhidonglizuo.ToString("0");
              
            }
        }

        private void xianshizhidongyou()
        {
            if (this.brake_right.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(xianshizhidongyou);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                this.brake_right.Visible = true;
                this.digitalGauge4.DigitCount = fzhidongliyou.ToString("0").Length < 3 ? 3 : fzhidongliyou.ToString("0").Length;
                this.digitalGauge4.Text = fzhidongliyou.ToString("0");
                
            }
        }

        protected void thisclose()
        {
            this.Invoke(new Action(this.Close));  //使用委托关闭该窗体
        }
        bool isEnd;    
        public void StratTest()
        {
            bool mj = true;//免检标识
            for (int i = 0; i < flowbass.iVax; i++)
            {
                if (flowbass.bJCXCZDZ[i] || flowbass.bJCZCZDZ[i])
                {
                    mj = false;
                }
            }
            if (mj)
            {
                flowbass.LedWrite("制动免检", "", 1);
                return;
            }
            GetIP();
            xianshineirong = "制动开始";
            xianshi();
            //flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
            Thread.Sleep(2000);
 
            isEnd = false;
            socketClient = new SocketHelper(ip, port);
            socketClient.RecData += new SocketHelper.RecEventHandler(DataAnalysis);
            socketClient.StartClient();
            Init();
            DateTime dstart = DateTime.Now;
            var datatime = 0;
            while (true)
            {
                if (flowbass.IsCancle())
                {
                    CancleFlow();
                    Thread.Sleep(500);
                    socketClient.CloseClient();
                    return;
                }
                if (isEnd)
                {
                    break;
                }
                if (m_mainform.DateDiff(DateTime.Now, dstart) > 1000)
                {
                    dstart = DateTime.Now;
                    datatime++;
                    xianshineirong = string.Format(@"制动开始{0}秒", datatime);
                    xianshi();
                    //flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                }
                Thread.Sleep(500);
            }
            socketClient.CloseClient();
            flowbass.getProcessfinish(flowbass.sDetectID, "B1");
            flowbass.getProcessfinish(flowbass.sDetectID, "B2");
            flowbass.getProcessfinish(flowbass.sDetectID, "B0");
            if (flowbass.iVax==3)
            {
                flowbass.getProcessfinish(flowbass.sDetectID, "B3");
            }

        }

        public void Thread1()
        {
            try
            {
                StratTest();
                if (jcxjs)
                {
                    xianshineirong = "检测线完成";
                    flowbass.UpdateLoginRecord(DateTime.Now.ToString(), "检测线完成", flowbass.sDetectID);
                    flowbass.LedWrite(flowbass.sPlate, xianshineirong, 1);
                }
                flowbass.LedWrite(flowbass.sgongwei, "", 1);
                xianshineirong = flowbass.sgongwei;
                xianshi();
                end();

            }
            catch (Exception ex)
            {
                //错误日记 记录到当天日期下目录中
                if (!(ex is System.Threading.ThreadAbortException))
                {
                    flowbass.LedWrite(flowbass.sPlate, "异常,停止检测", 1);
                    m_mainform.baocunriji("制动", ex.Message.ToString());
                    m_mainform.SaveErrorLog("制动检测流程", ex.Message.ToString());
                }
                flowbass.LedWrite(flowbass.sgongwei, "", 1);
                xianshineirong = flowbass.sgongwei;
                xianshi();
                end();
            }
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (flowbass.izhuangtai == 2)
            {
                m_mainform.objyibiaobass.select(flowbass.sshebei);
                m_mainform.bpanelshow = true;
                m_mainform.showpanel();
                try
                {
                    flowbass.izhuangtai = 1;
                    flowbass.IsReadyForStart();
                    t1 = new Thread(new ThreadStart(Thread1));
                    t1.Start();
                }
                catch (Exception ex)
                {

                    m_mainform.baocunriji("制动打开窗体失败", ex.Message.ToString());
                    m_mainform.SaveErrorLog("制动打开窗体", ex.Message.ToString());

                }
            }
        }

        public void timerstart()
        {
            timer1.Enabled = true;
        }

        public void end()
        {
            flowbass.izhuangtai = 0;
            flowbass.sendend("E", flowbass.sDetectID);
        }

        private void 仪表设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            工位配置.BRsetting obj = new 工位配置.BRsetting();
            obj.ShowDialog();
        }

        private void TESTBR_Shown(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
            this.BackgroundImage = Image.FromFile(@"./ico/制动背景.jpg");
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }

        private void Init()
        {
            string hphm = flowbass.sPlate;
            byte[] content = Encoding.Default.GetBytes(hphm);
            for (int i = content.Length; i < 20; i++)
            {
                hphm += " ";
            }
            byte[] data = DataPack("I", hphm);
            socketClient.SendMessage(data);
        }

        private void Start()
        {
            string zbzl = flowbass.iCLZBZL.ToString().PadLeft(5, ' ');
            string zzl = flowbass.iCLZZL.ToString().PadLeft(5, ' ');
            string zero = "   0";
            string qdlx = "4X2后驱后驻车";
            if (flowbass.iVax==3)
            {
                qdlx = "6X4后驱后驻车";
            }

            StringBuilder msg = new StringBuilder();
            if (flowbass.iVax == 3)
            {
                msg.Append("YYYNNN");//1~6:是否检测n轴制动
                msg.Append("NYYN");//7~10:1,2,3,4轴手刹
                msg.Append("Y");//11:整车手刹
                msg.Append("Y");//12:整车制动
                if (flowbass.absType == "-")
                {
                    msg.Append("NNNNNN");//13~18:是否检测n轴ABS
                }
                else
                {
                    msg.Append("YNYNNN");//13~18:是否检测n轴ABS
                }
      
                msg.Append(zbzl);//19~23:整备质量
                msg.Append(zzl);//24~28:总质量
                msg.Append("    0");//29~33:载质量
                msg.Append("3");//34:主车轴数
            }
            else
            {
                msg.Append("YYNNNN");//1~6:是否检测n轴制动
                msg.Append("NYNN");//7~10:1,2,3,4轴手刹
                msg.Append("Y");//11:整车手刹
                msg.Append("Y");//12:整车制动
                if (flowbass.absType == "-")
                {
                    msg.Append("NNNNNN");//13~18:是否检测n轴ABS
                }
                else
                {
                    msg.Append("YYNNNN");//13~18:是否检测n轴ABS
                }      
                msg.Append(zbzl);//19~23:整备质量
                msg.Append(zzl);//24~28:总质量
                msg.Append("    0");//29~33:载质量
                msg.Append("2");//34:主车轴数
            }
            msg.Append(zero);//35~38:一轴左轮质量
            msg.Append(zero);//39~42:一轴右轮质量
            msg.Append(zero);//43~46:二轴左轮质量
            msg.Append(zero);//47~50:二轴右轮质量
            msg.Append(zero);//51~54:三轴左轮质量
            msg.Append(zero);//55~58:三轴右轮质量
            msg.Append(zero);//59~62:四轴左轮质量
            msg.Append(zero);//63~66:四轴右轮质量
            msg.Append(zero);//67~70:五轴左轮质量
            msg.Append(zero);//71~74:五轴右轮质量
            msg.Append(zero);//75~78:六轴左轮质量
            msg.Append(zero);//79~82:六轴右轮质量
            msg.Append("0");//83:是否是双转向轴
            msg.Append("1");//84:车辆类型
            msg.Append(flowbass.absType == "-" ? "9" : flowbass.absType);//85:ABS型号
            msg.Append(qdlx.PadRight(26, ' '));//86~116:驱动形式
            byte[] data = DataPack("T", msg.ToString());
            socketClient.SendMessage(data);
        }

        private void CancleFlow()
        {
            byte[] data = DataPack("U", "");
            socketClient.SendMessage(data);
        }

        private void GetDataCmd()
        {
            byte[] data = DataPack("D", "D");
            socketClient.SendMessage(data);
        }

        public void DataAnalysis(byte[] buf)
        {
            if (buf.Length > 6)
            {
                byte lcr = DataLCR(buf);
                if (lcr == buf[buf.Length - 2])
                {
                    string cmd = Encoding.Default.GetString(buf, 4, buf.Length - 6);
                    if (cmd.Contains("I"))
                    {
                        Start();
                    }
                    else if (cmd.Contains("T0"))
                    {
                        GetDataCmd();
                    }
                    else if (cmd.Contains("DD"))
                    {
                        if (buf.Length == 616)
                        {
                            #region MyRegion
                            string data = Encoding.Default.GetString(buf, 4, 608);
                            if (data.Contains("F"))
                            {
                                flowbass.sethege(flowbass.sDetectID, "B0");
                                flowbass.sethege(flowbass.sDetectID, "B1");
                                flowbass.sethege(flowbass.sDetectID, "B2");
                                if (flowbass.iVax == 3)
                                {
                                    flowbass.sethege(flowbass.sDetectID, "B3");
                                }
                            }
                            else
                            {
                                flowbass.getbuhge(flowbass.sDetectID, "B0");
                                flowbass.getbuhge(flowbass.sDetectID, "B1");
                                flowbass.getbuhge(flowbass.sDetectID, "B2");
                                if (flowbass.iVax == 3)
                                {
                                    flowbass.getbuhge(flowbass.sDetectID, "B3");
                                }
                            }
                            string value;
                            #region 一轴
                            value = data.Substring(2, 5);//2~6：一轴左轮重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTZ1", "Detects_SJB", value.Trim());
                            value = data.Substring(7, 5);//7~11：一轴右轮重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTY1", "Detects_SJB", value.Trim());
                            value = data.Substring(12, 5);//12~16：一轴轴重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTH1", "Detects_SJB", value.Trim());
                            value = data.Substring(17, 5);//17~21：一轴制和左轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEL1", "Detects_SJB", value.Trim());
                            value = data.Substring(22, 5);//22~26：一轴制和右轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEY1", "Detects_SJB", value.Trim());
                            value = data.Substring(27, 5);//27~31：一轴制动因数(%)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEF1", "Detects_SJB", value.Trim());
                            value = data.Substring(32, 1);//32：一轴制动因数评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEFJL1", "Detects_SJB", GetResult(value));
                            value = data.Substring(33, 5);//33~37：一轴制差左轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEDIFZ1", "Detects_SJB", value.Trim());
                            value = data.Substring(38, 5);//38~42：一轴制差右轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEDIFY1", "Detects_SJB", value.Trim());
                            value = data.Substring(43, 5);//43~47：一轴制动力差占轴荷(%)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEDIFF1", "Detects_SJB", value.Trim());
                            value = data.Substring(48, 1);//48：一轴制动力差占轴荷评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEDIFFJL1", "Detects_SJB", GetResult(value));
                            value = data.Substring(49, 5);//49~53：一轴左阻滞力
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVZ1", "Detects_SJB", value.Trim());
                            value = data.Substring(54, 1);//54：一轴左阻滞力评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVZJL1", "Detects_SJB", GetResult(value));
                            value = data.Substring(55, 5);//55~59：一轴右阻滞力
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVY1", "Detects_SJB", value.Trim());
                            value = data.Substring(60, 1);//60：一轴右阻滞力评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVYJL1", "Detects_SJB", GetResult(value));
                            value = data.Substring(61, 5);//61~65：一轴协调时间
                            value = data.Substring(66, 1);//66：一轴协调时间评价
                            value = data.Substring(67, 1);//67：一轴制动总评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZJL1", "Detects_SJB", GetResult(value));
                            value = data.Substring(68, 1);//68：一轴ABS左阀的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQZ1", "Detects_SJB", GetResult(value));
                            value = data.Substring(69, 1);//69：一轴ABS右阀的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQY1", "Detects_SJB", GetResult(value));
                            value = data.Substring(70, 1);//70：一轴ABS阀是否装反评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQ1", "Detects_SJB", GetResult(value));
                            if (data.Substring(68, 3).Contains("F"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL1", "Detects_SJB", "2");
                            }
                            else if (data.Substring(68, 3).Contains("TTT"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL1", "Detects_SJB", "1");
                            }
                            else
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL1", "Detects_SJB", "0");
                            }
                            value = data.Substring(71, 1);//71：一轴ABS左速度的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQZ1", "Detects_SJB", GetResult(value));
                            value = data.Substring(72, 1);//72：一轴ABS右速度的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQY1", "Detects_SJB", GetResult(value));
                            value = data.Substring(73, 1);//73：一轴ABS速度是否装反评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQ1", "Detects_SJB", GetResult(value));
                            if (data.Substring(71, 3).Contains("F"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL1", "Detects_SJB", "2");
                            }
                            else if (data.Substring(71, 3).Contains("TTT"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL1", "Detects_SJB", "1");
                            }
                            else
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL1", "Detects_SJB", "0");
                            }
                            value = data.Substring(74, 1);//74：一轴ABS左传感器齿圈的测试评价
                            value = data.Substring(75, 1);//75：一轴ABS右传感器齿圈的测试评价
                            value = data.Substring(76, 1);//76：一轴ABS总评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSJL1", "Detects_SJB", GetResult(value));
                            value = data.Substring(77, 2);//77~78：一轴ABS左电磁阀故障
                            value = data.Substring(79, 2);//79~80：一轴ABS右电磁阀故障
                            value = data.Substring(81, 2);//81~82：一轴ABS左传感器故障
                            value = data.Substring(83, 2);//83~84：一轴ABS右传感器故障 
                            #endregion

                            #region 二轴
                            value = data.Substring(85, 5);//2~6：二轴左轮重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTZ2", "Detects_SJB", value.Trim());
                            value = data.Substring(90, 5);//7~11：二轴右轮重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTY2", "Detects_SJB", value.Trim());
                            value = data.Substring(95, 5);//12~16：二轴轴重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTH2", "Detects_SJB", value.Trim());
                            value = data.Substring(100, 5);//16~21：二轴制和左轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEL2", "Detects_SJB", value.Trim());
                            value = data.Substring(105, 5);//22~26：二轴制和右轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEY2", "Detects_SJB", value.Trim());
                            value = data.Substring(110, 5);//27~31：二轴制动因数(%)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEF2", "Detects_SJB", value.Trim());
                            value = data.Substring(115, 1);//32：二轴制动因数评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEFJL2", "Detects_SJB", GetResult(value));
                            value = data.Substring(116, 5);//33~37：二轴制差左轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEDIFZ2", "Detects_SJB", value.Trim());
                            value = data.Substring(121, 5);//38~42：二轴制差右轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEDIFY2", "Detects_SJB", value.Trim());
                            value = data.Substring(126, 5);//43~47：二轴制动力差占轴荷(%)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEDIFF2", "Detects_SJB", value.Trim());
                            value = data.Substring(131, 1);//48：二轴制动力差占轴荷评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEDIFFJL2", "Detects_SJB", GetResult(value));
                            value = data.Substring(132, 5);//49~53：二轴左阻滞力
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVZ2", "Detects_SJB", value.Trim());
                            value = data.Substring(137, 1);//54：二轴左阻滞力评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVZJL2", "Detects_SJB", GetResult(value));
                            value = data.Substring(138, 5);//55~59：二轴右阻滞力
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVY2", "Detects_SJB", value.Trim());
                            value = data.Substring(143, 1);//60：二轴右阻滞力评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVYJL2", "Detects_SJB", GetResult(value));
                            value = data.Substring(144, 5);//61~65：二轴协调时间
                            value = data.Substring(149, 1);//66：二轴协调时间评价
                            value = data.Substring(150, 1);//67：二轴制动总评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZJL2", "Detects_SJB", GetResult(value));
                            value = data.Substring(151, 1);//68：二轴ABS左阀的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQZ2", "Detects_SJB", GetResult(value));
                            value = data.Substring(152, 1);//69：二轴ABS右阀的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQY2", "Detects_SJB", GetResult(value));
                            value = data.Substring(153, 1);//70：二轴ABS阀是否装反评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQ2", "Detects_SJB", GetResult(value));
                            if (data.Substring(151, 3).Contains("F"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL2", "Detects_SJB", "2");
                            }
                            else if (data.Substring(151, 3).Contains("TTT"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL2", "Detects_SJB", "1");
                            }
                            else
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL2", "Detects_SJB", "0");
                            }
                            value = data.Substring(154, 1);//71：二轴ABS左速度的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQZ2", "Detects_SJB", GetResult(value));
                            value = data.Substring(155, 1);//72：二轴ABS右速度的测试评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQY2", "Detects_SJB", GetResult(value));
                            value = data.Substring(156, 1);//73：二轴ABS速度是否装反评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQ2", "Detects_SJB", GetResult(value));
                            if (data.Substring(154, 3).Contains("F"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL2", "Detects_SJB", "2");
                            }
                            else if (data.Substring(154, 3).Contains("TTT"))
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL2", "Detects_SJB", "1");
                            }
                            else
                            {
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL2", "Detects_SJB", "0");
                            }
                            value = data.Substring(157, 1);//74：二轴ABS左传感器齿圈的测试评价
                            value = data.Substring(158, 1);//75：二轴ABS右传感器齿圈的测试评价
                            value = data.Substring(159, 1);//76：二轴ABS总评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSJL2", "Detects_SJB", GetResult(value));
                            value = data.Substring(160, 2);//77~78：二轴ABS左电磁阀故障
                            value = data.Substring(162, 2);//79~80：二轴ABS右电磁阀故障
                            value = data.Substring(164, 2);//81~82：二轴ABS左传感器故障
                            value = data.Substring(166, 2);//83~84：二轴ABS右传感器故障 
                            #endregion

                            #region 三轴
                            if (flowbass.iVax == 3)
                            {
                                value = data.Substring(168, 5);//2~6：二轴左轮重
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTZ3", "Detects_SJB", value.Trim());
                                value = data.Substring(173, 5);//7~11：二轴右轮重
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTY3", "Detects_SJB", value.Trim());
                                value = data.Substring(178, 5);//12~16：二轴轴重
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTH3", "Detects_SJB", value.Trim());
                                value = data.Substring(183, 5);//16~21：二轴制和左轮制动(10N)
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEL3", "Detects_SJB", value.Trim());
                                value = data.Substring(188, 5);//22~26：二轴制和右轮制动(10N)
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEY3", "Detects_SJB", value.Trim());
                                value = data.Substring(193, 5);//27~31：二轴制动因数(%)
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEF3", "Detects_SJB", value.Trim());
                                value = data.Substring(198, 1);//32：二轴制动因数评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEFJL3", "Detects_SJB", GetResult(value));
                                value = data.Substring(199, 5);//33~37：二轴制差左轮制动(10N)
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEDIFZ3", "Detects_SJB", value.Trim());
                                value = data.Substring(204, 5);//38~42：二轴制差右轮制动(10N)
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEDIFY3", "Detects_SJB", value.Trim());
                                value = data.Substring(209, 5);//43~47：二轴制动力差占轴荷(%)
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEDIFF3", "Detects_SJB", value.Trim());
                                value = data.Substring(214, 1);//48：二轴制动力差占轴荷评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_BRIKEDIFFJL3", "Detects_SJB", GetResult(value));
                                value = data.Substring(215, 5);//49~53：二轴左阻滞力
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVZ3", "Detects_SJB", value.Trim());
                                value = data.Substring(220, 1);//54：二轴左阻滞力评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVZJL3", "Detects_SJB", GetResult(value));
                                value = data.Substring(221, 5);//55~59：二轴右阻滞力
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVY3", "Detects_SJB", value.Trim());
                                value = data.Substring(226, 1);//60：二轴右阻滞力评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZLVYJL33", "Detects_SJB", GetResult(value));
                                value = data.Substring(227, 5);//61~65：二轴协调时间
                                value = data.Substring(232, 1);//66：二轴协调时间评价
                                value = data.Substring(233, 1);//67：二轴制动总评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ZZJL3", "Detects_SJB", GetResult(value));
                                value = data.Substring(234, 1);//68：二轴ABS左阀的测试评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQZ2", "Detects_SJB", GetResult(value));
                                value = data.Substring(235, 1);//69：二轴ABS右阀的测试评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQY2", "Detects_SJB", GetResult(value));
                                value = data.Substring(236, 1);//70：二轴ABS阀是否装反评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQ2", "Detects_SJB", GetResult(value));
                                if (data.Substring(234, 3).Contains("F"))
                                {
                                    flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL2", "Detects_SJB", "2");
                                }
                                else if (data.Substring(234, 3).Contains("TTT"))
                                {
                                    flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL2", "Detects_SJB", "1");
                                }
                                else
                                {
                                    flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSTJQJL2", "Detects_SJB", "0");
                                }
                                value = data.Substring(237, 1);//71：二轴ABS左速度的测试评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQZ2", "Detects_SJB", GetResult(value));
                                value = data.Substring(238, 1);//72：二轴ABS右速度的测试评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQY2", "Detects_SJB", GetResult(value));
                                value = data.Substring(239, 1);//73：二轴ABS速度是否装反评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQ2", "Detects_SJB", GetResult(value));
                                if (data.Substring(237, 3).Contains("F"))
                                {
                                    flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL2", "Detects_SJB", "2");
                                }
                                else if (data.Substring(237, 3).Contains("TTT"))
                                {
                                    flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL2", "Detects_SJB", "1");
                                }
                                else
                                {
                                    flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSLSCGQJL2", "Detects_SJB", "0");
                                }
                                value = data.Substring(240, 1);//74：二轴ABS左传感器齿圈的测试评价
                                value = data.Substring(241, 1);//75：二轴ABS右传感器齿圈的测试评价
                                value = data.Substring(242, 1);//76：二轴ABS总评价
                                flowbass.AddData_Check(flowbass.sDetectID, "DET_ABSJL2", "Detects_SJB", GetResult(value));
                                value = data.Substring(243, 2);//77~78：二轴ABS左电磁阀故障
                                value = data.Substring(245, 2);//79~80：二轴ABS右电磁阀故障
                                value = data.Substring(247, 2);//81~82：二轴ABS左传感器故障
                                value = data.Substring(249, 2);//83~84：二轴ABS右传感器故障 
                            }

                            #endregion

                            value = data.Substring(501, 2);//501~502：ASR阀的故障

                            value = data.Substring(566, 5);//566~570：整车轴重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTHZC", "Detects_SJB", value.Trim());
                            int brakeL = 0, brakeR = 0;
                          
                            value = data.Substring(571, 5);//571~575：整车制和左轮制动(10N)    
                            int.TryParse(value, out brakeL);
                            value = data.Substring(576, 5);//576~580：整车制和右轮制动(10N)
                            int.TryParse(value, out brakeR);
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKE", "Detects_SJB", (brakeL + brakeR).ToString());
                            value = data.Substring(581, 5);//581~585：整车制和
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_BRAKEF", "Detects_SJB", value.Trim());
                            value = data.Substring(586, 1);//586：整车制动评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_ZCJL", "Detects_SJB", GetResult(value));
                            value = data.Substring(587, 5);//587~591：整车手刹轴重
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_WEIGHTHSS", "Detects_SJB", value.Trim());
                            value = data.Substring(592, 5);//592~596：整车手刹制和左轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_HALTZH", "Detects_SJB", value.Trim());
                            value = data.Substring(597, 5);//597~601：整车手刹制和右轮制动(10N)
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_HALTYH", "Detects_SJB", value.Trim());
                            value = data.Substring(602, 5);//602~606：整车手刹制和
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_HALTF", "Detects_SJB", value.Trim());
                            value = data.Substring(607, 1);//607：整车手刹制动评价
                            flowbass.AddData_Check(flowbass.sDetectID, "DET_SSJL", "Detects_SJB", GetResult(value));
                            #endregion
                            isEnd = true ;
                        }
                    }

                }
            }

        }

        private string GetResult(string value)
        {
            switch (value)
            {
                case "T": return "1";
                case "F": return "2";
                case "N": return "2";
                case "-": return "0";
                default: return "0";             
            }
        }

        public byte[] DataPack(string command, string content)
        {
            byte[] datacommand = Encoding.Default.GetBytes(command);
            byte[] data = Encoding.Default.GetBytes(content);
            byte[] datalength = new byte[2];
            UInt16 result = (UInt16)(data.Length + 1);
            datalength[0] = (byte)(result & 0xff);//低位
            datalength[1] = (byte)(result >> 8);//高位


            List<byte> cmd = new List<byte>();
            cmd.Add(0x02);
            cmd.Add(0x01);
            cmd.AddRange(datalength);
            cmd.AddRange(datacommand);
            cmd.AddRange(data);
            cmd.Add(DataLCR(datalength, datacommand, data));
            cmd.Add(0x03);
            byte[] array = new byte[cmd.Count];
            cmd.CopyTo(array);
            return array;
        }

        /// <summary>
        /// LCR校验
        /// </summary>
        /// <param name="datalength"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte DataLCR(byte[] datalength, byte[] datacommand, byte[] data)
        {
            byte uSum = 0x01;
            for (int i = 0; i < datalength.Length; i++)
            {
                uSum += (byte)(datalength[i]);
            }
            for (int i = 0; i < datacommand.Length; i++)
            {
                uSum += (byte)(datacommand[i]);
            }
            for (int i = 0; i < data.Length; i++)
            {
                uSum += (byte)(data[i]);
            }

            return uSum;

        }

        public byte DataLCR(byte[] data)
        {
            byte uSum = new byte();
            for (int i = 1; i < data.Length - 2; i++)
            {
                uSum += (byte)(data[i]);
            }

            return uSum;

        }

    }
}
